package lx.af.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import lx.af.R;
import lx.af.base.BaseActivity;
import lx.af.manager.GlobalThreadManager;

/**
 * demo for in.srain.cube.ptr, pull to refresh. see detail here:
 * https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh
 */
public class ActivityPullToRefreshDemo extends BaseActivity
        implements BaseActivity.ActionBarImpl {

    private static final String[] COLORS = new String[] {
            "#ffb957",
            "#24ae60",
            "#49cbc9",
            "#9acd34",
            "#ff5757",
    };

    private PtrClassicFrameLayout mPtrLayout;
    private TextView mTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_to_refresh_demo);
        mTxt = getView(R.id.activity_pull_to_refresh_txt);
        mPtrLayout = getView(R.id.activity_pull_to_refresh_ptr);

        mPtrLayout.setLastUpdateTimeRelateObject(this);
        mPtrLayout.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // begin refresh operation
                refreshTxt();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
    }

    private void refreshTxt() {
        GlobalThreadManager.runInThreadPool(new Runnable() {
            @Override
            public void run() {
                final String txt = getRefreshTxt();
                mTxt.post(new Runnable() {
                    @Override
                    public void run() {
                        Random random = new Random();
                        mTxt.setTextColor(Color.parseColor(COLORS[random.nextInt(5)]));
                        mTxt.setText(txt);
                        // refresh finished, remember to set ptr as complete.
                        mPtrLayout.refreshComplete();
                    }
                });
            }
        });
    }

    private String getRefreshTxt() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 20; i++) {
            sb.append(getCurrentTime()).append("\n");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    private static String getCurrentTime() {
        DateFormat mFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        return mFormatter.format(new Date());
    }
}
